Security News
UK Officials Consider Banning Ransomware Payments from Public Entities
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.
react-swipeable
Advanced tools
The react-swipeable package is a React component that provides easy-to-use swipe event handlers for touch devices. It allows developers to add swipe functionality to their React components, making it ideal for creating touch-friendly user interfaces.
Basic Swipe Detection
This code demonstrates how to use the react-swipeable package to detect basic swipe events. The `useSwipeable` hook is used to create swipe handlers, which are then applied to a div element. When a swipe is detected, a message is logged to the console.
import React from 'react';
import { useSwipeable } from 'react-swipeable';
const SwipeComponent = () => {
const handlers = useSwipeable({
onSwiped: (eventData) => console.log('User Swiped!', eventData)
});
return (
<div {...handlers} style={{ width: '100%', height: '100px', background: 'lightgray' }}>
Swipe here
</div>
);
};
export default SwipeComponent;
Swipe Direction Detection
This code demonstrates how to detect the direction of a swipe using the react-swipeable package. The `useSwipeable` hook is configured with handlers for each swipe direction (left, right, up, down), and logs a message to the console when a swipe in that direction is detected.
import React from 'react';
import { useSwipeable } from 'react-swipeable';
const SwipeDirectionComponent = () => {
const handlers = useSwipeable({
onSwipedLeft: () => console.log('Swiped Left!'),
onSwipedRight: () => console.log('Swiped Right!'),
onSwipedUp: () => console.log('Swiped Up!'),
onSwipedDown: () => console.log('Swiped Down!')
});
return (
<div {...handlers} style={{ width: '100%', height: '100px', background: 'lightblue' }}>
Swipe in any direction
</div>
);
};
export default SwipeDirectionComponent;
Swipe Threshold Configuration
This code demonstrates how to configure a swipe threshold using the react-swipeable package. The `useSwipeable` hook is configured with a `delta` value, which sets the minimum distance (in pixels) that a swipe must cover to be detected.
import React from 'react';
import { useSwipeable } from 'react-swipeable';
const SwipeThresholdComponent = () => {
const handlers = useSwipeable({
onSwiped: (eventData) => console.log('User Swiped!', eventData),
delta: 50 // Minimum distance (in pixels) for a swipe to be detected
});
return (
<div {...handlers} style={{ width: '100%', height: '100px', background: 'lightgreen' }}>
Swipe with a minimum threshold
</div>
);
};
export default SwipeThresholdComponent;
The react-swipe package provides a React component for touch slide navigation. It is similar to react-swipeable in that it allows for swipe detection, but it is more focused on creating swipeable carousels and sliders. It offers a higher-level abstraction compared to react-swipeable.
The react-use-gesture package is a set of hooks for handling gestures in React. It supports a wide range of gestures, including swipes, pinches, and scrolls. Compared to react-swipeable, react-use-gesture offers more comprehensive gesture support and can be used for more complex interactions.
The react-swipeable-views package is a React component for creating swipeable views, such as tabs or carousels. It is similar to react-swipeable in that it provides swipe detection, but it is specifically designed for creating swipeable view containers. It offers built-in support for animations and transitions.
React swipe component - Swipe bindings for react
Using npm:
$ npm install --save react-swipeable
react-swipeable generates a React component (defaults to <div>
) and binds touch events to it.
var Swipeable = require('react-swipeable')
var SampleComponent = React.createClass({
render: function () {
return (
<Swipeable
onSwiping={this.swiping}
onSwipingUp={this.swipingUp}
onSwipingRight={this.swipingRight}
onSwipingDown={this.swipingDown}
onSwipingLeft={this.swipingLeft}
onSwiped={this.swiped}
onSwipedUp={this.swipedUp}
onSwipedRight={this.swipedRight}
onSwipedDown={this.swipedDown}
onSwipedLeft={this.swipedLeft}
onTap={this.tapped} >
You can swipe here!
</Swipeable>
)
}
})
onSwiping
, onSwipingUp
, onSwipingRight
, onSwipingDown
, onSwipingLeft
, are called with the event
as well as the absolute delta of where the swipe started and where it's currently at. These constantly fire throughout touch events.
onSwiping
in addition to the swipe delta, onSwiping
also returns the current absolute X and Y position, as well as the current velocity of the swipe. this.props.onSwiping(e, deltaX, deltaY, absX, absY, velocity)
onSwipedUp
, onSwipedRight
, onSwipedDown
, onSwipedLeft
are called with the event
as well as the x distance, + or -, from where the swipe started to where it ended. These only fire at the end of a touch event.
onSwiped
is called with the event, the X and Y delta, whether or not the event was a flick, and the current velocity of the swipe. this.props.onSwiped(e, x, y, isFlick, velocity)
onTap
is called with the onTouchEnd event when the element has been tapped. this.props.onTap(e)
#####Configuration Props
flickThreshold
is a number (float) which determines the max velocity of a swipe before it's considered a flick. The default value is 0.6
.
delta
is the amount of px before we start firing events. Also affects how far onSwipedUp
, onSwipedRight
, onSwipedDown
, and onSwipedLeft
need to be before they fire events. The default value is 10
.
preventDefaultTouchmoveEvent
is whether to prevent the browser's touchmove event. Sometimes you would like the target to scroll natively. The default value is true
.
stopPropagation
automatically calls stopPropagation on all 'swipe' events. The default value is false
.
nodeName
is a string which determines the html element/node that this react component binds its touch events to then returns. The default value is 'div'
.
trackMouse
will allow mouse 'swipes' to be tracked(click, hold, move, let go). See #51 for more details. The default value is false
.
None of the props are required.
onSwiped: React.PropTypes.func,
onSwiping: React.PropTypes.func,
onSwipingUp: React.PropTypes.func,
onSwipingRight: React.PropTypes.func,
onSwipingDown: React.PropTypes.func,
onSwipingLeft: React.PropTypes.func,
onSwipedUp: React.PropTypes.func,
onSwipedRight: React.PropTypes.func,
onSwipedDown: React.PropTypes.func,
onSwipedLeft: React.PropTypes.func,
onTap: React.PropTypes.func,
flickThreshold: React.PropTypes.number,
delta: React.PropTypes.number,
preventDefaultTouchmoveEvent: React.PropTypes.bool,
stopPropagation: React.PropTypes.bool,
nodeName: React.PropTypes.string
trackMouse: React.PropTypes.bool,
Initial set up, run npm install
.
Make changes/updates to the src/Swipeable.js
file.
Before creating a PR please run npm test
to make sure the tests and lint pass. Add tests if PR adds functionality please.
cd into examples
directory, then npm install
within that directory, and finally npm start
.
After the server starts you can then view the examples page with your changes at http://localhost:3000
.
You can now make updates/changes to src/Swipeable.js
and webpack will rebuild, then reload the page so you can test your changes!
MIT
FAQs
React Swipe event handler hook
The npm package react-swipeable receives a total of 317,456 weekly downloads. As such, react-swipeable popularity was classified as popular.
We found that react-swipeable demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 16 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.